package com.situ.mall.admin.service.impl;

import com.situ.mall.admin.entity.AdminMenu;
import com.situ.mall.base.exception.InfoException;
import com.situ.mall.admin.mapper.AdminMenuMapper;
import com.situ.mall.admin.service.IAdminMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.mall.admin.vo.AdminMenuVO;
import com.situ.mall.admin.vo.HomeInfoVO;
import com.situ.mall.admin.vo.InitVO;
import com.situ.mall.base.vo.LayuiTableVO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author darling
 * @since 2023-03-17
 */
@Service
public class AdminMenuServiceImpl extends ServiceImpl<AdminMenuMapper, AdminMenu> implements IAdminMenuService {

    @Override
    public InitVO init() {
        InitVO vo = new InitVO();
        vo.setHomeInfo(new HomeInfoVO("首页","/home/page"));
        Map<String, String> logoMap = new HashMap<>();
        logoMap.put("title", "商城后台管理");
        logoMap.put("image", "images/favicon.ico");
        logoMap.put("href", "");
        vo.setLogoInfo(logoMap);
        // 有几个一级菜单，就返回几条数据
        List<AdminMenuVO> list = new ArrayList<>();
        // 查询所有一级菜单
        vo.setMenuInfo(baseMapper.menuInfo());
        return vo;
    }

    @Override
    public LayuiTableVO doList() {
        return LayuiTableVO.data(baseMapper.selectList(null));
    }

    @Override
    public void doDel(Integer id) {
        // id参数是要删除的菜单的ID
        List<AdminMenu> list = baseMapper.selectByParentId(id);// 查此菜单有没有子菜单
        if (list != null && list.size() > 0) {// 有  --> 错误提示
            throw new InfoException("存在子菜单,不可删除！", 500);
        }
        baseMapper.deleteById(id);// 没有 -> 删除
    }
}
